package com.example.aicooking.controller;

import com.example.aicooking.entity.vo.RefrigeratorFoodVO;
import com.example.aicooking.service.RefrigeratorFoodService;
import com.example.aicooking.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/api/refrigerator-foods")
public class RefrigeratorFoodController {

    @Autowired
    private RefrigeratorFoodService refrigeratorFoodService;

    @GetMapping("/list")
    public Result<List<RefrigeratorFoodVO>> list(@RequestParam Long userId) {
        return Result.success(refrigeratorFoodService.getFoodList(userId));
    }

    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id, @RequestParam Long userId) {
        try {
            boolean success = refrigeratorFoodService.deleteFood(id, userId);
            if (success) {
                return Result.success("删除成功");  // 返回成功消息
            } else {
                return Result.error("删除失败，食材不存在或无权限删除");
            }
        } catch (Exception e) {
            log.error("删除食材失败", e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }
}
